package com.mdd.admin.controller;

import com.mdd.admin.aop.Log;
import com.mdd.admin.service.IUserService;
import com.mdd.admin.validate.UserCreateValidate;
import com.mdd.admin.validate.UserSearchValidate;
import com.mdd.admin.validate.UserUpdateRoleValidate;
import com.mdd.admin.validate.UserUpdateValidate;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.UserDetailVo;
import com.mdd.admin.vo.UserListedVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("api/user")
@Api(tags = "用户数据管理")
public class UserController {

    @Resource
    IUserService iUserService;

    @GetMapping("/list")
    @ApiOperation(value="用户信息列表")
    public AjaxResult<PageResult<UserListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated UserSearchValidate searchValidate) {
        PageResult<UserListedVo> list = iUserService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="用户信息详情")
    public AjaxResult<UserDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        UserDetailVo detail = iUserService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "用户信息新增")
    @PostMapping("/add")
    @ApiOperation(value="用户信息新增")
    public AjaxResult<Object> add(@Validated @RequestBody UserCreateValidate createValidate) {
        iUserService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "用户信息编辑")
    @PostMapping("/edit")
    @ApiOperation(value="用户信息编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody UserUpdateValidate updateValidate) {
        iUserService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "用户角色编辑")
    @PostMapping("/editRole")
    @ApiOperation(value="用户角色编辑")
    public AjaxResult<Object> editRole(@Validated @RequestBody UserUpdateRoleValidate updateValidate) {
        iUserService.editRole(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "用户信息删除")
    @PostMapping("/del")
    @ApiOperation(value="用户信息删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iUserService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
